
import axios from 'axios'
import { Message } from "element-ui";
// const BASE_URL = '/inspect'
// window.BASE_URL = '/digitalsite_crm'
const service = axios.create({
  // baseURL: window.BASE_URL,
  baseURL: '',
  timeout: 10 * 1000
})
// import { Toast } from 'vant';
let token = ''
// let token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxNDQ4MTAxMzg4MTQ5MjU2MTkzIiwia2luZCI6NCwiaXNzIjoiMTQ0ODEwMTM4ODA1Mjc4NzIwMiIsImV4cCI6MTk0NTIzMzM5NX0.nT7m4ZbwOm8TY5K5_3S_rof2kxB2mBNP49g-x8t5olQ'
// let token = sessionStorage.getItem('Authorization')
// console.log(token);
axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";
// request拦截器
service.interceptors.request.use(
  config => {
    // console.log(config, 'config')
    // 是否需要设置 token
    // const isToken = (config.headers || {}).isToken === false;
    // if (getToken() && !isToken) {
    config.headers['Authorization'] = token // 让每个请求携带自定义token 请根据实际情况自行修改// }
    // get请求映射params参数
    if (config.method === 'get' && config.params) {
      let url = config.url + '?'
      for (const propName of Object.keys(config.params)) {
        const value = config.params[propName]
        var part = encodeURIComponent(propName) + '='
        if (value !== null && typeof value !== 'undefined') {
          if (typeof value === 'object') {
            for (const key of Object.keys(value)) {
              let params = propName + '[' + key + ']'
              var subPart = encodeURIComponent(params) + '='
              url += subPart + encodeURIComponent(value[key]) + '&'
            }
          } else {
            url += part + encodeURIComponent(value) + '&'
          }
        }
      }
      url = url.slice(0, -1)
      config.params = {}
      config.url = url
    }
    return config
  },
  error => {
    console.log(error)
    Promise.reject(error)
  }
)
service.interceptors.response.use(res => {
  return new Promise((resolve, reject) => {
    // 未设置状态码则默认成功状态
    const code = +res.data.code || 200
    if (code === 200) {
      resolve(res.data)
    }
    // console.log(code,'code');
    if (code === 400 || code === 500 || code === 600) {
      // console.log(123456, res.data);
      Message({ type: 'error', message: res.data.msg })
      reject(res.data)
    }
    resolve(res.data)
  })
}, error => {
  return Promise.reject(error, 'shiba')
})

export default service
